package com.dark.sys.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import com.dark.common.repository.hibernate.HibernateDao;
import com.dark.common.utils.BeanUtils;
import com.dark.common.web.model.DataGrid;
import com.dark.common.web.model.Page;
import com.dark.sys.entity.Menu;
import com.dark.sys.service.MenuService;
import com.dark.sys.web.dto.MenuDto;

@Service
public class MenuServiceImpl extends HibernateDao<Menu, Integer> implements MenuService{
	
	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public void add(MenuDto dto) {
		changeSelected(dto);
		Menu entity = new Menu() ;
		BeanUtils.copyNotNullProperties(dto, entity);
		super.save(entity); 
	}
	
	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public void delete(String ids) {
		if(null != ids && ids.length() > 0) {
			String[] split = ids.split(",") ;
			Map<String, Object> alias = new HashMap<String, Object>() ;
			alias.put("ids", split) ;
			super.executeSQL("delete from sys_menu where id in(:ids)", alias) ;
		}
	}
	
	public void changeSelected(MenuDto dto) {
		if(dto.getSelected()) {
			super.executeSQL("update sys_menu set selected=false") ;
		}
	}

	@CacheEvict(value="SystemMenuCache", allEntries=true)
	@Override
	public void update(MenuDto dto) {
		changeSelected(dto);
		
		Menu entity = this.getEntity(dto.getId());
		BeanUtils.copyNotNullProperties(dto, entity);
		super.update(entity);
	}

	@Cacheable(value="SystemMenuCache", key="#root.targetClass + (\"#MenuId[\"+#id+\"]\")")
	@Override
	public MenuDto get(Integer id) {
		Menu entity = super.getEntity(id);
		MenuDto dto = new MenuDto() ;
		BeanUtils.copyNotNullProperties(entity, dto);
		return dto;
	}
	
	
	
	@Override
	public DataGrid datagrid(MenuDto dto) {
		DataGrid dg = new DataGrid() ;
		
		Page<MenuDto> findByPage = this.findByPage(dto) ;
		dg.setTotal(((Integer)findByPage.getTotalNumber()).longValue());
		dg.setRows(findByPage.getDataRows()) ;
		
		return dg;
	}
	
	@Cacheable(value="SystemMenuCache", key="#root.targetClass")
	@Override
	public List<MenuDto> listAll(MenuDto dto) {
		StringBuffer queryString = new StringBuffer("SELECT t.* FROM sys_menu t WHERE 1=1") ;
		
		Map<String, Object> alias = new HashMap<String, Object>() ;
		queryString = addWhere(queryString, alias, dto) ;
		
		return super.findListSQL(queryString.toString(), alias, MenuDto.class, false) ;
	}
	

	public Page<MenuDto> findByPage(MenuDto dto) {
		StringBuffer queryString = new StringBuffer("SELECT t.* FROM sys_menu t WHERE 1=1") ;
		
		Map<String, Object> alias = new HashMap<String, Object>() ;
		queryString = addWhere(queryString, alias, dto) ;
		
		return super.findPageSQL(queryString.toString(), alias, Menu.class, false) ;
	}
	
	private StringBuffer addWhere(StringBuffer queryString, Map<String, Object> alias, MenuDto dto) {
		if(null != dto) {
			if(null != dto.getId()) {
				queryString.append(" AND t.id=:id") ;
				alias.put("id", dto.getId()) ;
			}
			if(dto.getQuickSearch() != null && !"".equals(dto.getQuickSearch())){
				queryString.append(" and ")
				.append("(")
				.append("t.name like '%%" + dto.getQuickSearch() + "%%'")
				.append(")");
			}
		}
		return queryString;
	}

}
